<?php
class socketController{
    private $action;
    private $floor;
    private $num;
    private $c;
    private $host = '192.168.100.236';
    private $port=5432;
    private $socket;
    private $build_temp=array(
        'a'=>array('7.4','7.11','7.8','8.4','8.5','8.12','8.15','9.6','12.3','9.15','10.9','11.1','11.6','11.7','11.16','12.7','12.12','6.7'),
        'b'=>array('7.2','7.12','7.10','8.3','8.7','8.14','9.2','9.4','12.5','10.3','10.10','10.14','11.3','11.11','11.13','12.9','12.13','6.9'),
        'c'=>array('7.3','7.13','7.6','8.2','8.8','8.13','9.1','9.5','12.4','9.16','10.8','10.15','11.4','11.10','11.14','12.10','12.15','6.4'),
        'd'=>array('7.5','7.14','7.7','7.16','8.6','8.10','8.16','9.8','12.1','10.4','10.12','10.16','11.2','11.8','11.12','12.6','12.11','6.8'),
        'e'=>array('7.1','7.15','7.9','8.1','8.9','8.11','9.3','9.7','12.2','10.6','10.11','10.13','11.5','11.9','11.15','12.8','12.14',6.6),
    );
    public function __construct(){
        $this->c=$_GET['c']? $_GET['c']: '';
        $this->action=$_GET['action']? $_GET['action']: '';
        $this->floor = $_GET['floor']? $_GET['floor']: '';
        $this->num =  $_GET['num']? $_GET['num']: '';
    }
    public function actionRoute(){
        switch($this->action)
        {
            case 'ynit'://独立户型控制器
                $this->actionYnit();
                break;
            case 'level'://楼层控制
                $this->actionLevel();
                break;
            case 'all'://1-16整层
                $this->actionAll();
                break;
            default:
                echo '控制器参数必填';
        }
    }
    //独立户型控制器
    private function actionYnit()
    {
        $nums=$this->build_temp[$this->num][$this->floor] or die('参数传递错误');
        $nums=str_replace('.',';',$nums);
        $msg="NOnOff:".$nums."#";
        echo $msg;
        $this->funSocket($msg);
    }
    //楼层控制器
    private function actionLevel()
    {
        $this->openSocket();
        foreach ($this->build_temp as $k => $v) {
            $nums=str_replace('.',';',$v[$this->floor]);
            $msg="NOnOff:".$nums."#".'<br/>';
            $this->writeSocket($msg);
        }
        print_r($msg);
        $this->closeSocket();
    }
    //整体开关控制器
    private function actionAll(){
        if($this->c=='on'){
            $msg="NAO:1&2&3&4&5&6&7&8&9&&10&11&12&13&14&15&16#";
        }else{
            $msg="NAC:1&2&3&4&5&6&7&8&9&&10&11&12&13&14&15&16#";
        }
        echo $msg;
        $this->funSocket($msg);
    }

    //建立通讯接口
    private function funSocket($msg){
        set_time_limit(0);
        //创建一个socket
        $this->socket=socket_create(AF_INET,SOCK_STREAM,SOL_TCP)or die("cannot create socket\n");
        //进行接口连接
        $conn=socket_connect($this->socket,$this->host,$this->port) or die("cannot connect server\n");
        if($conn){echo "client connect ok!";}
        //向接口写数据
        $write=socket_write($this->socket,$msg) or die("cannot write data\n");
        //关闭接口
        socket_close($this->socket);
    }
    private function openSocket(){
        set_time_limit(0);
        $socket=socket_create(AF_INET,SOCK_STREAM,SOL_TCP)or die("cannot create socket\n");
        $conn=socket_connect($socket,$this->host,$this->port) or die("cannot connect server\n");
        if($conn){echo "client connect ok!";}
    }
    private function writeSocket($msg){
        $write=socket_write($this->socket,$msg) or die("cannot write data\n");
    }
    private function closeSocket(){
        socket_close($this->socket);
    }
}
$socket=new socketController();
$socket->actionRoute();
?>